<?php
class ControllerCommonContentMaintop extends Controller {
	public function index() {
      		$this->load->model('design/layout');
		$this->load->model('catalog/category');
		$this->load->model('catalog/product');
		$this->load->model('catalog/information');

		if (isset($this->request->get['route'])) {
			$route = $this->request->get['route'];
		} else {
			$route = 'common/home';
		}

		$layout_id = 0;

		if (substr($route, 0, 16) == 'product/category' && isset($this->request->get['path'])) {
			$path = explode('_', (string)$this->request->get['path']);

			$layout_id = $this->model_catalog_category->getCategoryLayoutId(end($path));
		}

		if (substr($route, 0, 16) == 'product/product' && isset($this->request->get['product_id'])) {
			$layout_id = $this->model_catalog_product->getProductLayoutId($this->request->get['product_id']);
		}

		if (substr($route, 0, 16) == 'product/information' && isset($this->request->get['information_id'])) {
			$layout_id = $this->model_catalog_information->getInformationLayoutId($this->request->get['information_id']);
		}

		if (!$layout_id) {
			$layout_id = $this->model_design_layout->getLayout($route);
		}

		if (!$layout_id) {
			$layout_id = $this->config->get('config_layout_id');
		}

		$module_data = array();
		
		$this->load->model('setting/extension');
		
		$extensions = $this->model_setting_extension->getExtensions('module');		
		
		foreach ($extensions as $extension) {
			$modules = explode(',', $this->config->get($extension['code'] . '_module'));
		
			foreach ($modules as $module) {
               //ac block
			   $layout_module=$this->config->get($extension['code'] . '_' . $module . '_layout_id');
               $layout_module=  explode(',',$layout_module);
				if (in_array($layout_id,$layout_module) && $this->config->get($extension['code'] . '_' . $module . '_position') == 'content_maintop' && $this->config->get($extension['code'] . '_' . $module . '_status'))
                {
                    // ---- Su ly thiet lam]p tham so
                    $paramurl=trim($this->config->get($extension['code'] . '_' . $module . '_layout_paramurl'));
				    if(!empty($paramurl))// kiem tra xem module co thiet lap hien thi theo paramurl khong
                    {
                     $paramurl=explode(';',$paramurl);
                     foreach($paramurl as $p)
                     {
                        $p=explode('=',$p);
                        if(count($p)>1)
                        {
                            //echo '<br>Lay tham so:'.$p[0];
                            $urlparam = $this->request->get[$p[0]];   // lay tham so dc thiet lap

                          if(in_array($urlparam,explode(',',$p[1]))) // kiem tra xem gia tri hien thoi co trung voi thiet lap
                          {
                            // echo '<br>co';print_r($p);
                             	$module_data[] = array(
      						'code'       => $extension['code'],
      						'module'     => $module,
      						'sort_order' => $this->config->get($extension['code'] . '_' . $module . '_sort_order')
      				    	);
                          }

                       }
                      }
                    }
                    else
                    {
    					$module_data[] = array(
    						'code'       => $extension['code'],
    						'module'     => $module,
    						'sort_order' => $this->config->get($extension['code'] . '_' . $module . '_sort_order')
    					);
                    }
                    // ----E_ Su ly thiet lam]p tham so
				}
			}
		}
		
		$sort_order = array(); 
	  
		foreach ($module_data as $key => $value) {
      		$sort_order[$key] = $value['sort_order'];
    	}
		
		array_multisort($sort_order, SORT_ASC, $module_data);
		
		$this->data['modules'] = array();
		
		foreach ($module_data as $module) {
			$module = $this->getChild('module/' . $module['code'], $module['module']);
			
			if ($module) {
				$this->data['modules'][] = $module;
			}
		}

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/content_maintop.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/content_maintop.tpl';
		} else {
			$this->template = 'default/template/common/content_maintop.tpl';
		}
								
		$this->render();
	}
}
?>